package com.dcm.live.service.impl;

import com.dcm.common.constant.Constants;
import com.dcm.common.exception.MyException;
import com.dcm.live.entity.LiveItemEntity;
import com.dcm.live.mapper.LiveItemMapper;
import com.dcm.live.service.LiveItemService;
import com.dcm.live.vo.DataAnalysisEntity;
import com.dcm.live.vo.FrontDeskEntity;
import com.dcm.live.vo.LiveRoomdtoEntity;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * [直播商品 业务 实现类]
 *
 * @author : [pengrongsheng]
 * @createTime : [2021/3/28]
 */
@Service
public class LiveItemServiceImpl implements LiveItemService {


    @Autowired
    private LiveItemMapper liveItemMapper;

    @Override
    public List<LiveItemEntity> findAll() {
        return liveItemMapper.findAll();
    }

    @Override
    public LiveItemEntity findItemById(Long id) {
        if (id == null || id < 0) {
            throw new MyException(Constants.ResponseMsg.VALIDATE_FAILED);
        }
        return liveItemMapper.findItemById(id);
    }

    @Override
    public void addItem(LiveItemEntity item) {
        checkLiveItem(item);
        liveItemMapper.addItem(item);
    }

    @Override
    public List<LiveItemEntity> findItemByKeyWords(String keyWords) {
        return liveItemMapper.findItemByKeyWords(keyWords);
    }


    @Override
    public void updateItemStatus(LiveItemEntity item) {
        if (item.getId() == null || item.getId() < 0) {
            throw new MyException(Constants.LiveItemErrorMsg.LIVE_ID_CANNOT_NULL);
        }
        liveItemMapper.updateItemStatus(item);
    }


    @Override
    public PageInfo<LiveItemEntity> findPage(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<LiveItemEntity> list = liveItemMapper.findAll();
        PageInfo<LiveItemEntity> page = new PageInfo<>(list);
        return page;
    }

    Integer status = 0;
    @Override
    public DataAnalysisEntity dataAnalysis(Long id) {
        DataAnalysisEntity dataAnalysisEntity = new DataAnalysisEntity();
//        下单量
        status = 0;
        Integer live = liveItemMapper.findNumber(id,status);
        dataAnalysisEntity.setPaceorder(live);
//        成交
        status = 1;
        Integer live2 = liveItemMapper.findNumber(id,status);
        dataAnalysisEntity.setTurnover(live2);
//       收货
        status = 3;
        Integer live3 = liveItemMapper.findNumber(id,status);
        dataAnalysisEntity.setDelivery(live3);
//        退货
        status = 4;
        Integer live4 = liveItemMapper.findNumber(id,status);
        dataAnalysisEntity.setSalesreturn(live4);

        return dataAnalysisEntity;
    }

    @Override
    public FrontDeskEntity fondest(Long id) {
        return liveItemMapper.frontDeskEntity(id);
    }

    @Override
    public void establishBroadcasting(LiveRoomdtoEntity liveRoomDtoEntity) {
        liveItemMapper.establishBroadcasting(liveRoomDtoEntity);
    }

    /**
     * 检查实体类参数
     * @param item
     */
    private static void checkLiveItem(LiveItemEntity item)  {
        if (item.getItemId() == null || item.getItemId() < 0) {
            throw new MyException(Constants.LiveItemErrorMsg.ITEM_ID_CANNOT_NULL);
        }
        if (StringUtils.isBlank(item.getName())) {
            throw new MyException(Constants.LiveItemErrorMsg.LIVE_NAME_CANNOT_NULL);
        }
        if (item.getLivePrice() == null || item.getLivePrice().compareTo(BigDecimal.ZERO) < 0) {
            throw new MyException(Constants.LiveItemErrorMsg.LIVE_PRICE_CANNOT_NULL);
        }
        if (StringUtils.isBlank(item.getImage())) {
            throw new MyException(Constants.LiveItemErrorMsg.LIVE_IMAGE_CANNOT_NULL);
        }
    }
}
